package com.deep.test.thread.concurrent;

/**
 * <p>Description:</p>
 * Created with IDEA
 * author:hudepin
 * createTime:2018/7/5 15:17
 */
public class ThreadState {
    public static void main(String[] args) {
        new Thread(new TimeWaiting(),"TimeWaitingThread").start();
        new Thread(new Waiting(),"WaitingThread").start();
        new Thread(new Blocked(),"BlockedThread1").start();
        new Thread(new Blocked(),"BlockedThread2").start();
    }
    //该线程不断的进行睡眠
    static class TimeWaiting implements Runnable{

        @Override
        public void run() {
            while(true){
                SleepUtils.sleep(100);
            }
        }
    }
    //该线程一直在Wating.class 实例上等待
    static class Waiting implements Runnable{

        @Override
        public void run() {
            while(true){
                synchronized(Waiting.class){
                    try {
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    //该线程在Blocked.class实例上加锁后，不会释放锁
    static class Blocked implements Runnable{
        @Override
        public void run() {
            synchronized (Blocked.class){
                while(true){
                    SleepUtils.sleep(100);
                }
            }
        }
    }
    /**
     * jps 命令查看正在运行java进程，jstack pid dump线程信息如下
     *
     * Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.161-b12 mixed mode):

     "DestroyJavaVM" #15 prio=5 os_prio=0 tid=0x0000000002984000 nid=0x2984 waiting o
     n condition [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "BlockedThread2" #14 prio=5 os_prio=0 tid=0x000000001900d000 nid=0x31ac waiting
     for monitor entry [0x0000000019c9e000]
     java.lang.Thread.State: BLOCKED (on object monitor)
     at com.deep.test.thread.concurrent.ThreadState$Blocked.run(ThreadState.j
     ava:48)
     - waiting to lock <0x00000000d60e1180> (a java.lang.Class for com.deep.t
     est.thread.concurrent.ThreadState$Blocked)
     at java.lang.Thread.run(Thread.java:748)

     "BlockedThread1" #13 prio=5 os_prio=0 tid=0x000000001900c000 nid=0x2a1c waiting
     on condition [0x0000000019b9e000]
     java.lang.Thread.State: TIMED_WAITING (sleeping)
     at java.lang.Thread.sleep(Native Method)
     at java.lang.Thread.sleep(Thread.java:340)
     at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
     at com.deep.test.thread.concurrent.SleepUtils.sleep(SleepUtils.java:14)
     at com.deep.test.thread.concurrent.ThreadState$Blocked.run(ThreadState.j
     ava:48)
     - locked <0x00000000d60e1180> (a java.lang.Class for com.deep.test.threa
     d.concurrent.ThreadState$Blocked)
     at java.lang.Thread.run(Thread.java:748)

     "WaitingThread" #12 prio=5 os_prio=0 tid=0x0000000018fff000 nid=0x351c in Object
     .wait() [0x0000000019a9f000]
     java.lang.Thread.State: WAITING (on object monitor)
     at java.lang.Object.wait(Native Method)
     - waiting on <0x00000000d60dee10> (a java.lang.Class for com.deep.test.t
     hread.concurrent.ThreadState$Waiting)
     at java.lang.Object.wait(Object.java:502)
     at com.deep.test.thread.concurrent.ThreadState$Waiting.run(ThreadState.j
     ava:34)
     - locked <0x00000000d60dee10> (a java.lang.Class for com.deep.test.threa
     d.concurrent.ThreadState$Waiting)
     at java.lang.Thread.run(Thread.java:748)

     "TimeWaitingThread" #11 prio=5 os_prio=0 tid=0x0000000018ffc000 nid=0x3040 waiti
     ng on condition [0x000000001999f000]
     java.lang.Thread.State: TIMED_WAITING (sleeping)
     at java.lang.Thread.sleep(Native Method)
     at java.lang.Thread.sleep(Thread.java:340)
     at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
     at com.deep.test.thread.concurrent.SleepUtils.sleep(SleepUtils.java:14)
     at com.deep.test.thread.concurrent.ThreadState$TimeWaiting.run(ThreadSta
     te.java:22)
     at java.lang.Thread.run(Thread.java:748)

     "Service Thread" #10 daemon prio=9 os_prio=0 tid=0x0000000018e56800 nid=0x33cc r
     unnable [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x0000000018dd4800 nid=0x30f
     0 waiting on condition [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x0000000018da9800 nid=0x267
     c waiting on condition [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000000018da6800 nid=0x371
     4 waiting on condition [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x0000000018d38800 nid=0x149
     c runnable [0x000000001939e000]
     java.lang.Thread.State: RUNNABLE
     at java.net.SocketInputStream.socketRead0(Native Method)
     at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
     at java.net.SocketInputStream.read(SocketInputStream.java:171)
     at java.net.SocketInputStream.read(SocketInputStream.java:141)
     at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
     at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
     at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
     - locked <0x00000000d6132f00> (a java.io.InputStreamReader)
     at java.io.InputStreamReader.read(InputStreamReader.java:184)
     at java.io.BufferedReader.fill(BufferedReader.java:161)
     at java.io.BufferedReader.readLine(BufferedReader.java:324)
     - locked <0x00000000d6132f00> (a java.io.InputStreamReader)
     at java.io.BufferedReader.readLine(BufferedReader.java:389)
     at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:
     64)

     "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000017607800 nid=0x1250 w
     aiting on condition [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000018cb0800 nid=0x3448
     runnable [0x0000000000000000]
     java.lang.Thread.State: RUNNABLE

     "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000002a77800 nid=0x21c0 in Obje
     ct.wait() [0x000000001890f000]
     java.lang.Thread.State: WAITING (on object monitor)
     at java.lang.Object.wait(Native Method)
     - waiting on <0x00000000d5f08ec0> (a java.lang.ref.ReferenceQueue$Lock)
     at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
     - locked <0x00000000d5f08ec0> (a java.lang.ref.ReferenceQueue$Lock)
     at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
     at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

     "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000017579000 nid=0x326
     4 in Object.wait() [0x000000001880f000]
     java.lang.Thread.State: WAITING (on object monitor)
     at java.lang.Object.wait(Native Method)
     - waiting on <0x00000000d5f06b68> (a java.lang.ref.Reference$Lock)
     at java.lang.Object.wait(Object.java:502)
     at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
     - locked <0x00000000d5f06b68> (a java.lang.ref.Reference$Lock)
     at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

     "VM Thread" os_prio=2 tid=0x0000000017577800 nid=0x32bc runnable

     "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002999800 nid=0x30a8 runn
     able

     "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000299b000 nid=0x353c runn
     able

     "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000299d800 nid=0x30c4 runn
     able

     "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000299f000 nid=0x35e4 runn
     able

     "VM Periodic Task Thread" os_prio=2 tid=0x0000000018e59800 nid=0x131c waiting on
     condition

     JNI global references: 30

     */
}
